#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n + 1), b(m + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= m; i++)
    {
        cin >> b[i];
    }
    int tt = 2;
    int l = 1, j = 1;
    for (l = 1, j = 1; l <= n && j <= m; l++)
    {
        if (a[l] == b[j])
        {
            // cout << l << ":";
            tt--;
            if (tt == 0)
            {
                tt = 2;
                j++;
                // cout << j << ":";
                if (j == m + 1)
                    break;
            }
        }
        else
        {
            continue;
        }
    }
    bool ok = false;
    // cout << ":::::::::" << j << endl;
    if (j == m + 1)
    {
        cout << "Yes\n";
        return 0;
        // cout << "Yes\n";
    }
    int cnt = 0;
    for (l = 1, j = 1; l <= n && j <= m; l++)
    {
        if (a[l] == b[j])
        {
            j++;
            if (j == m + 1)
            {
                j = 1;
                cnt++;
                if (cnt == 2)
                {
                    ok = true;
                    cout << "Yes\n";
                    break;
                }
            }
        }
        else
        {
            continue;
        }
    }

    return 0;
}